package com.ruoyi.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.base.domain.bo.BaHehUseLogBo;
import com.ruoyi.base.domain.vo.BaHehUseLogVo;
import com.ruoyi.base.domain.BaHehUseLog;
import com.ruoyi.base.mapper.BaHehUseLogMapper;
import com.ruoyi.base.service.IBaHehUseLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 盒号使用记录Service业务层处理
 *
 * @author zyb
 * @date 2024-06-19
 */
@RequiredArgsConstructor
@Service
public class BaHehUseLogServiceImpl implements IBaHehUseLogService {

    private final BaHehUseLogMapper baseMapper;

    /**
     * 查询盒号使用记录
     */
    @Override
    public BaHehUseLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询盒号使用记录列表
     */
    @Override
    public TableDataInfo<BaHehUseLogVo> queryPageList(BaHehUseLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaHehUseLog> lqw = buildQueryWrapper(bo);
        Page<BaHehUseLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询盒号使用记录列表
     */
    @Override
    public List<BaHehUseLogVo> queryList(BaHehUseLogBo bo) {
        LambdaQueryWrapper<BaHehUseLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaHehUseLog> buildQueryWrapper(BaHehUseLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaHehUseLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getHehId() != null, BaHehUseLog::getHehId, bo.getHehId());
        lqw.eq(bo.getUseDate() != null, BaHehUseLog::getUseDate, bo.getUseDate());
        lqw.eq(bo.getReportId() != null, BaHehUseLog::getReportId, bo.getReportId());
        lqw.eq(bo.getDeptId() != null, BaHehUseLog::getDeptId, bo.getDeptId());
        lqw.eq(bo.getExcelId() != null, BaHehUseLog::getExcelId, bo.getExcelId());
        return lqw;
    }

    /**
     * 新增盒号使用记录
     */
    @Override
    public Boolean insertByBo(BaHehUseLogBo bo) {
        BaHehUseLog add = BeanUtil.toBean(bo, BaHehUseLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改盒号使用记录
     */
    @Override
    public Boolean updateByBo(BaHehUseLogBo bo) {
        BaHehUseLog update = BeanUtil.toBean(bo, BaHehUseLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaHehUseLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除盒号使用记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
